package com.wantong.jianpu.viewmodels;

import android.app.Application;
import android.app.Person;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.wantong.jianpu.activitys.FaultCreateActivity;
import com.wantong.jianpu.activitys.FaultDetailActivity;
import com.wantong.jianpu.adapters.FaultRepairAdapter;
import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.beans.FaultListBean;
import com.wantong.jianpu.beans.PersonBean;
import com.wantong.jianpu.beans.RankBean;
import com.wantong.jianpu.beans.StatusBean;
import com.wantong.jianpu.models.FaultModel;
import com.wantong.jianpu.net.BaseObserver;
import com.wantong.jianpu.utils.Constants;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author PACY
 * @date 2022/4/25
 * description:
 */
public class FaultRepairViewModel extends BaseViewModel {
    public FaultModel model;
    public FaultRepairAdapter mAdapter;
    private int pageSize=20;
    private int pageNum=1;
    public SingleLiveEvent<Boolean> refreshEvent=new SingleLiveEvent<>();
    //维修等级
    public List<RankBean> rankBeans;
    public SingleLiveEvent<String> rank=new SingleLiveEvent<>();
    public SingleLiveEvent<String[]> rankDialogEvent=new SingleLiveEvent<>();
    public String maintainLevelId;//维修等级id
    //维修人员
    public List<PersonBean> userBeans;
    public SingleLiveEvent<String> user=new SingleLiveEvent<>();
    public SingleLiveEvent<String[]> userDialogEvent=new SingleLiveEvent<>();
    public int repairUser;//维修人员id
    //维修状态
    public List<StatusBean> stateBeans;
    public SingleLiveEvent<String> state=new SingleLiveEvent<>();
    public SingleLiveEvent<String[]> stateDialogEvent=new SingleLiveEvent<>();
    public int status;//维修状态id



    public FaultRepairViewModel(@NonNull Application application) {
        super(application);
        model=new FaultModel();
    }

    @Override
    protected void init() {
        super.init();
        initAdapter();
        getRankRequest();
        getUserRequest();
        getStateRequest();
        loadData(true);
    }


    public void initAdapter(){
        mAdapter=new FaultRepairAdapter();
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            loadData(false);
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Bundle bundle=new Bundle();
            bundle.putString("id",mAdapter.getItem(position).getId());
            startActivity(FaultDetailActivity.class,bundle);
        });

    }

    /**
     * 创建故障
     */
    public BindingCommand toCreatePage=new BindingCommand(() -> {
        Bundle bundle=new Bundle();
        bundle.putBoolean("isCreate",true);
        startActivity(FaultCreateActivity.class,bundle);
    });


    /**
     * 筛选维修等级
     */
    public BindingCommand selectRank=new BindingCommand(() -> {
        if (rankBeans!= null) {
            String[] brand = new String[rankBeans.size()];
            for (int i = 0; i < rankBeans.size(); i++) {
                brand[i] = rankBeans.get(i).getName();
            }
            rankDialogEvent.setValue(brand);
        } else {
            getRankRequest();
        }
    });

    /**
     * 维修等级选择
     */
    public void setRankPosition(int position) {
        if (rankBeans != null) {
            rank.setValue(rankBeans.get(position).getName());
            maintainLevelId=rankBeans.get(position).getId();
            loadData(true);
        }
    }


    /**
     * 筛选维修人员
     */
    public BindingCommand selectUser=new BindingCommand(() -> {
        if (userBeans!= null) {
            String[] brand = new String[userBeans.size()];
            for (int i = 0; i < userBeans.size(); i++) {
                brand[i] = userBeans.get(i).getName();
            }
            userDialogEvent.setValue(brand);
        } else {
            getUserRequest();
        }
    });
    /**
     * 维修人员选择
     */
    public void setUserPosition(int position) {
        if (userBeans != null) {
            user.setValue(userBeans.get(position).getName());
            repairUser=userBeans.get(position).getId();
            loadData(true);
        }
    }

    /**
     * 筛选维修状态
     */
    public BindingCommand selectState=new BindingCommand(() -> {
        if (stateBeans!= null) {
            String[] brand = new String[stateBeans.size()];
            for (int i = 0; i < stateBeans.size(); i++) {
                brand[i] = stateBeans.get(i).getDictLabel();
            }
            stateDialogEvent.setValue(brand);
        } else {
            getStateRequest();
        }
    });
    /**
     * 维修状态选择
     */
    public void setStatePosition(int position) {
        if (stateBeans != null) {
            state.setValue(stateBeans.get(position).getDictLabel());
            status=Integer.parseInt(stateBeans.get(position).getDictValue());
            loadData(true);
        }
    }




    /**
     * 获取维修等级
     */
    public void getRankRequest(){
        showDialog();
        model.rankList(new BaseObserver<List<RankBean>>() {
            @Override
            public void onSuccess(BaseResultBean<List<RankBean>> t) {
                rankBeans=t.getData();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }

    /**
     * 获取维修等级
     */
    public void getUserRequest(){
        showDialog();
        model.userList(new BaseObserver<List<PersonBean>>() {
            @Override
            public void onSuccess(BaseResultBean<List<PersonBean>> t) {
                userBeans=t.getData();
            }
            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }

    /**
     * 获取维修状态
     */
    public void getStateRequest(){
        showDialog();
        model.statusList(new BaseObserver<List<StatusBean>>() {
            @Override
            public void onSuccess(BaseResultBean<List<StatusBean>> t) {
                stateBeans=t.getData();
            }
            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }

    /**
     * 查询维修列表
     */
    public void loadData(boolean isRefresh){
        if(isRefresh){
            pageNum=1;
            refreshEvent.setValue(true);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        if(StringUtils.isNotEmpty(maintainLevelId)){
            map.put("maintainLevelId",maintainLevelId);
        }
        if(repairUser!=0){
            map.put("repairUser",repairUser);
        }
        if(status!=0){
            map.put("status",status);
        }

        model.faultList(map, new BaseObserver<FaultListBean>() {
            @Override
            public void onSuccess(BaseResultBean<FaultListBean> t) {
                if(isRefresh){
                    if(t.getData().getRows().isEmpty()){
                        showEmpty();
                    }else{
                        showContent();
                        mAdapter.setNewInstance(t.getData().getRows());
                    }
                }else{
                    mAdapter.addData(t.getData().getRows());
                    mAdapter.getLoadMoreModule().loadMoreComplete();
                    if(t.getData().getRows().size()<pageSize){
                        mAdapter.getLoadMoreModule().loadMoreEnd();
                    }
                }
                pageNum++;
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mAdapter.getLoadMoreModule().loadMoreFail();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                refreshEvent.setValue(false);
            }
        },getLifecycleProvider());
    }


}
